package com.czp.ssm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.czp.ssm.bean.Aircraft;
import com.czp.ssm.bean.Opera;
import com.czp.ssm.bean.OperaRule;
import com.czp.ssm.bean.PlaneOrder;
import com.czp.ssm.bean.PlaneOrderExample;
import com.czp.ssm.bean.Position;
import com.czp.ssm.bean.Train;
import com.czp.ssm.bean.TrainExample;
import com.czp.ssm.bean.TrainExample.Criteria;
import com.czp.ssm.bean.TrainOrder;
import com.czp.ssm.bean.TrainOrderExample;
import com.czp.ssm.bean.User;
import com.czp.ssm.dao.CompanyMapper;
import com.czp.ssm.dao.OperaMapper;
import com.czp.ssm.dao.OperaRuleMapper;
import com.czp.ssm.dao.TrainMapper;
import com.czp.ssm.dao.TrainOrderMapper;
import com.czp.ssm.dao.UserMapper;
import com.czp.ssm.service.OperaService;
import com.czp.ssm.service.TrainService;
import com.czp.ssm.utils.CommonUtils;
@Service
public class TrainServiceImpl implements TrainService {

	@Autowired
	private TrainMapper trainMapper;
	
	@Autowired
	private TrainOrderMapper trainOrderMapper;
	
	@Autowired
	private OperaService operaService;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private CompanyMapper companyMapper;
	
	@Autowired
	private OperaMapper operaMapper;
	
	@Autowired
	private OperaRuleMapper operaRuleMapper;
	
	public List<Train> getTrainListByCondition(Train train) {  
		TrainExample trainExample = new TrainExample();
		trainExample.setOrderByClause("TRAIN_ID asc");
		trainExample.setDistinct(false);
		Criteria criteria = trainExample.createCriteria();
		criteria.andStartCityNameEqualTo(train.getStartCityName());
		criteria.andEndCityNameEqualTo(train.getEndCityName());
		criteria.andStartDateEqualTo(train.getStartDate());
		List<Train> trainList = trainMapper.selectByExample(trainExample);
		for (int i = 0; i < trainList.size(); i++) {
			String position = trainList.get(i).getTrainPosition();
			JSONArray jsonArray = JSONArray.parseArray(position) ;
			List<Position> positions = jsonArray.toJavaList(Position.class);
			trainList.get(i).setPositionList(positions);
		}
		return trainList;
	}


	public Train getTrainByCondition(String trainId, String positonId) {
		Train train = trainMapper.selectByTrainId(trainId);
		String position = train.getTrainPosition();
		JSONArray jsonArray = JSONArray.parseArray(position) ;
		List<Position> positions = jsonArray.toJavaList(Position.class);
		System.out.println("positions---------"+positions);
		for (int i = 0; i < positions.size(); i++) {
			Position p = positions.get(i);
			if (positonId.equals(p.getPositionId())) {
				train.setPosition(p);
				break;
			}
		}
		return train;
	}


	public int saveTrainOrder(TrainOrder trainOrder) {
		trainOrder.setOrderId(CommonUtils.getOrderId());
		trainOrder.setOrderStatus("0");//待审批
		trainOrder.setPayType("0");///为付款
		trainOrder.setIsquit("0");//是否改签过
		trainOrder.setCreateOrderDate(new Date());
		//获取对应的审批人ID
		Opera opera = operaService.getOperaPersonIdByCompanyIdAndUserId(trainOrder.getCompanyId(),trainOrder.getUserId());
		trainOrder.setOperaPersonId(opera.getOperaPersonId());
		int flag = trainOrderMapper.insert(trainOrder);
		return flag;
	}


	public List<TrainOrder> findAllPlaneOrders() {
		TrainOrderExample example = new TrainOrderExample();
		example.setOrderByClause("ID asc");
		example.setDistinct(false);
		List<TrainOrder> trainList = trainOrderMapper.selectByExample(example);
		for (int i = 0; i < trainList.size(); i++) {
			TrainOrder trainOrder = trainList.get(i);
			User user = userMapper.selectUserByUserId(trainOrder.getUserId());
			trainOrder.setUserName(user.getUserName());
			trainOrder.setLinkPhone(user.getUserPhone());
			Map<String, String> map = new HashMap<String, String>();
			map.put("COMPANY_ID", trainOrder.getCompanyId());
			map.put("USER_ID", trainOrder.getUserId());
			Opera opera = operaMapper.selectOperaPersonIdByCompanyIdAndUserId(map);
			User operaUser = userMapper.selectUserByUserId(opera.getOperaPersonId());
			trainOrder.setOperaPersonName(operaUser.getUserName());
		}
		return trainList;
	}


	public List<TrainOrder> findTrainOrderByOperaPersonId(String operaPersonId) {
		List<TrainOrder> trainOrders = new ArrayList<TrainOrder>();
		List<Opera> operas = operaMapper.selectOperaByOperaPersonId(operaPersonId);
		for (int i = 0; i < operas.size(); i++) {
			Opera opera = operas.get(i);
			List<TrainOrder> trainOrderList = trainOrderMapper.selectTrainOrderListByOpera(opera);
			if (trainOrderList != null && trainOrderList.size()>0) {
				for (int j = 0; j < trainOrderList.size(); j++) {
					TrainOrder trainOrder = trainOrderList.get(j);
					User user = userMapper.selectUserByUserId(opera.getUserId());
					trainOrder.setLinkPhone(user.getUserPhone());
					OperaRule operaRule = operaRuleMapper.selectByOperaRuleId(opera.getOperaRuleId());
					trainOrder.setOperaRuleContent(operaRule.getOperaRuleContent());
					trainOrders.add(trainOrder);
				}
			}
		}
		return trainOrders;
	}


	public int updateTrainOrderStatus(String id, String status) {
		TrainOrder trainOrder = new TrainOrder();
		trainOrder.setId(Long.parseLong(id));
		trainOrder.setOrderStatus(status);
		int flag = trainOrderMapper.updateByPrimaryKeySelective(trainOrder);
		return flag;
	}
}
